package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.VehicleMapper;
import cn.tedu._03vehicle.pojo.dto.VehicleAddParam;
import cn.tedu._03vehicle.pojo.dto.VehicleListQuery;
import cn.tedu._03vehicle.pojo.entity.Vehicle;
import cn.tedu._03vehicle.pojo.vo.UserLoginVo;
import cn.tedu._03vehicle.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "车辆管理")
@Slf4j
@RestController
@Validated
@RequestMapping("/v1/vehicle")
public class VehicleController {
    @Autowired(required = false)
    private VehicleMapper vehicleMapper;

    @PostMapping("/add")
    @ApiOperation("添加车辆")
    @ApiOperationSupport(order = 10)
    public JsonResult add(@RequestBody VehicleAddParam vehicleAddParam,
                          @ApiIgnore HttpSession session) {
        log.debug("vehicleAddParam=" + vehicleAddParam);
        UserLoginVo userLoginVo = (UserLoginVo) session.getAttribute("user");
       /* if (userLoginVo == null) {
            return new JsonResult(StatusCode.NOT_LOGIN);
        }*/
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam, vehicle);

        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId(userLoginVo.getId());
        log.debug("vehicle={}", vehicle);
        vehicleMapper.insertVehicle(vehicle);
        return JsonResult.ok();
    }

    @GetMapping("/list")
    @ApiOperation("查询车辆列表")
    @ApiOperationSupport(order = 20)
    public JsonResult list(VehicleListQuery vehicleListQuery,
                           @ApiIgnore HttpSession session) {
        log.debug("vehicleListQuery=" + vehicleListQuery);
        UserLoginVo userLoginVo = (UserLoginVo) session.getAttribute("user");
        /*if (userLoginVo == null) {
            return new JsonResult(StatusCode.NOT_LOGIN);
        }*/
        String brand = vehicleListQuery.getBrand();
        String license = vehicleListQuery.getLicense();
        Long userId = userLoginVo.getId();
        List<VehicleListVO> selectVehicleList = vehicleMapper.selectVehicleList(brand, license, userId);

        return JsonResult.ok(selectVehicleList);
    }

    @GetMapping("/del/{id}")
    @ApiOperation("删除车辆")
    @ApiOperationSupport(order = 30)
    @ApiImplicitParam(name = "id", value = "车辆id", required = true, dataType = "Long")
//    @RequestParam
    public JsonResult delete(@Range(min = 0, max = 10000, message = "id为0到10000") @PathVariable Long id, @ApiIgnore HttpSession session) {
        log.debug("id=" + id);
        if (id < 0) {
            throw new IllegalArgumentException("id不能小于0");
        }
        UserLoginVo userLoginVo = (UserLoginVo) session.getAttribute("user");
        /*if (userLoginVo == null) {
            return new JsonResult(StatusCode.NOT_LOGIN);
        }*/
        int num = vehicleMapper.deleteVehicle(id);
        if (num > 0) {
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED, "删除失败,用户不存在");
    }
}
